<?php
/**
 * 后台通知
 */

namespace app\common\model;


use think\Db;

class AdminNotify extends  Model
{
    const ORDER_GOODS =31; //预约下单
    const USER_WITHDRAW =13; //用户提现

    const BATTERY_ORDER_GOODS =231;
    const BATTERY_ORDER_CANCEL=232;
    const TABLE_NAME = 'admin_notify';
    const CACHE_EXPIRE_LEVEL = self::CACHE_EXPIRE_LEVEL_4;

    protected $name = self::TABLE_NAME;

    public static function incrNotifyNum($type,$num=1)
    {

        $admin_ids = Db::name('admin')->column('admin_id');

        $query = self::commonQuery(self::TABLE_NAME);
        if($admin_ids)
        {
            $update_new_num_sql="insert into admin_notify(admin_id,notify_type,notify_num) values(:user_id,$type,$num)  on duplicate key update notify_num=notify_num+$num";

            foreach ($admin_ids as $v)
            {
                $query->execute($update_new_num_sql,['user_id'=>$v]);

            }
        }

    }
    public static function descNotifyNum($type,$num=1)
    {
        self::commonQuery(self::TABLE_NAME)
            ->where(['notify_type'=>$type])
            ->exp('notify_num',"greatest(0, notify_num - $num)")
            ->update();
    }
    public static function getAdminNotifyNum($admin_id)
    {
        return self::commonQuery(self::TABLE_NAME)
               ->where(['admin_id'=>$admin_id])
               ->column('notify_type,notify_num','notify_type');
    }
    public static function clearAdminNotifyNum($admin_id,$type)
    {
        self::commonQuery(self::TABLE_NAME)
            ->where(['admin_id'=>$admin_id, 'notify_type'=>$type])
             ->setField('notify_num',0);
    }
}